﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using RuleCode.Table;

namespace chatblog
{
    public partial class blogedit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                cbuser.dealcookie();
                dealrequest();
                bind1();
                binduser();
                //bind2();
                //bind3();
            }
        }
        /// <summary>
        /// 绑定用户信息
        /// </summary>
        private void binduser()
        {
            if (AliApi.appUserId == "")
            {
                Label_kyjf.Visible = false;
            }
            else
            {
                cbuser u = cbuser.Find(" cbuserid = '" + AliApi.appUserId + "'");
                Label_kyjf.Visible = true;
                Label_kyjf.Text = u.cbpoint.ToString();
            }
        }
        /// <summary>
        /// 绑定查看的帖子
        /// </summary>
        private void bind1()
        {
            HyperLink_existfile.Visible = false;
            Button_clearfile.Visible = false;
            cbusercata uc = cbusercata.Find(" cbuserid = '" + AliApi.appUserId + "'", "RECID");
            DropDownList_cata.Items.Clear();
            foreach (DataRow item in uc.MyTable.Rows)
            {
                DropDownList_cata.Items.Add(item["cbusercataid"].ToString());
            }
            string sql = " select 省编码 from 省 ";
            DataTable dt = RuleCode.Common.SqlHelper.Instance.ExecuteQuery(sql).Tables[0];
            DropDownList_area.Items.Clear();
            DropDownList_area.Items.Add("");
            foreach (DataRow item in dt.Rows)
            {
                DropDownList_area.Items.Add(item[0].ToString());
            }
            cbuser u = cbuser.Find(" cbuserid = '" + AliApi.appUserId + "'");
            DropDownList_area.Text = u.area.ToString();
            cbusergroup ug = cbusergroup.Find(" cbuserid = '" + AliApi.appUserId + "'");
                DropDownList_group.Items.Clear();
                DropDownList_group.Items.Add("无");
                DropDownList_group.Items.Add("全部好友");

                foreach (DataRow item in ug.MyTable.Rows)
                {
                    DropDownList_group.Items.Add(item["cbname"].ToString());
                }
            if (this.Request.Params["cblistid"] != null)
            {
                
                cblist l = cblist.Find(" cblistid = '" + this.Request.Params["cblistid"].ToString() + "' ");
                if (l.cbpath.ToString() != "")
                {
                    HyperLink_existfile.Visible = true;
                    HyperLink_existfile.NavigateUrl = l.cbpath.ToString();
                    HyperLink_existfile.Text = "现有附件:" + l.cbpath.ToString();
                    Button_clearfile.Visible = true;
                }
                if (l.Count > 0)
                {
                    TextBox_title.Text = l.cbtitle.ToString();
                    DropDownList_cata.Text = l.cbusercataid.ToString();
                    Fck_Content.Value = Server.HtmlDecode(l.cbcontent.ToString());
                    DropDownList_group.Text = l.cbusergroupid.ToString();
                    DropDownList_area.Text = l.area.ToString();
                    if (l.isprivate.Value == RuleCode.Enum.NoYes.Yes)
                    {
                        CheckBox_pub.Checked = true;
                    }
                    else
                    {
                        CheckBox_pub.Checked = false;
                    }
                    TextBox_point.Text = l.fenshu.ToString();
                    TextBox_point.Enabled = true;
                }
                else
                {
                    TextBox_point.Text = "0";
                    TextBox_point.Enabled = false;
                }

            }
        }

        protected void Button_ok_Click(object sender, EventArgs e)
        {
            string strImg2 = FileUpload_fj.FileName.Substring(FileUpload_fj.FileName.LastIndexOf(".") + 1);
            string time2 = DateTime.Now.ToString("yyyyMMddhhmmss") + "." + strImg2;
            string strFilePth = Server.MapPath(@"~/file/") + time2;
            string linkpth = ResolveUrl(@"~/file/") + time2;
            if (FileUpload_fj.HasFile)
            {
                FileUpload_fj.SaveAs(strFilePth);
            }



            if (this.Request.Params["cblistid"] != null)
            {
                cblist l = cblist.Find(" cblistid = '" + this.Request.Params["cblistid"].ToString() + "' ");
                l.cbtitle.Value = TextBox_title.Text;
                l.cbcontent.Value = Fck_Content.Value;
                l.jcmodedate.Value = DateTime.Now;
                l.jccreatetime.StringValue = DateTime.Now.ToLongTimeString();
                l.cbusercataid.StringValue = DropDownList_cata.Text;
                l.cbusergroupid.Value = DropDownList_group.Text;
                if (l.cbusergroupid.ToString() != "无" && l.cbusergroupid.ToString() != "全部好友")
                {
                    cbusergroup ug2 = cbusergroup.Find(" cbname = '" + DropDownList_group.SelectedItem.Value + "' ");
                    l.cbusergroupid.Value = ug2.cbusergroupid.ToString();
                }
                l.area.Value = DropDownList_area.Text;
                //l.cbuserid.Value = AliApi.appUserId;修改人
                if (FileUpload_fj.HasFile)
                {
                    l.cbpath.Value = linkpth;
                }

                cbusercata uc = cbusercata.Find(" cbuserid = '" + AliApi.appUserId+"' "+
                    " and cbusercataid = '" + l.cbusercataid.StringValue+"'");
                if (uc.cbcataid.ToString()!="100")
                {
                    l.cbcataid.Value = uc.cbcataid.Value;                    
                }

                
                if (CheckBox_pub.Checked)
                {
                    l.isprivate.Value = RuleCode.Enum.NoYes.Yes;
                }
                else
                {
                    l.isprivate.Value = RuleCode.Enum.NoYes.No;
                }
                l.Modify();
                ///发送邮件
                if ((l.isprivate.Value == RuleCode.Enum.NoYes.No) 
                    && (l.cblisttype.ToString() == "发帖"))
                {
                    cblist.sendemail(l);
                }
                Response.Redirect(ResolveUrl("~/blogview/") + l.cblistid.ToString()+".html");
            }
            else
            {
                //给分不能为负数
                if (zfunction.str2int(TextBox_point.Text) < 0)
                {
                    MessageBox.Show(this.Page, "给分不能为负数.");
                    return;
                }
                //检查已有积分
                if (zfunction.str2int(TextBox_point.Text) > 0)
                {
                    cbuser u = cbuser.Find(" cbuserid = '" + AliApi.appUserId + "' ");
                    if (u.cbpoint.Value < zfunction.str2int(TextBox_point.Text))
                    {
                        MessageBox.Show(this.Page, "你的积分余额不够.");
                        return;
                    }
                }
                cblist l = cblist.Find(" 1=2 ");
                l.New();
                l.cbtitle.Value = TextBox_title.Text;
                l.cbcontent.Value = Fck_Content.Value;
                l.cblistid.Value = l.GetRecID().ToString();
                l.jccreatedate.Value = DateTime.Now;
                l.jccreatetime.StringValue = DateTime.Now.ToLongTimeString();
                l.cbusercataid.StringValue = DropDownList_cata.Text;
                l.cbusergroupid.Value = DropDownList_group.Text;
                if (l.cbusergroupid.ToString() != "无" && l.cbusergroupid.ToString() != "全部好友")
                {
                    cbusergroup ug2 = cbusergroup.Find(" cbname = '" + DropDownList_group.SelectedItem.Value + "' ");
                    l.cbusergroupid.Value = ug2.cbusergroupid.ToString();
                }
                l.area.Value = DropDownList_area.Text;
                l.cbuserid.Value = AliApi.appUserId;
                l.cbstatus.Value = "有效";
                l.cblisttype.Value = "发帖";
                l.fenshu.Value = zfunction.str2int(TextBox_point.Text);
                if (FileUpload_fj.HasFile)
                {
                    l.cbpath.Value = linkpth;
                }
                cbusercata uc = cbusercata.Find(" cbuserid = '" + AliApi.appUserId + "' " +
                    " and cbusercataid = '" + l.cbusercataid.StringValue + "'");
                l.cbcataid.Value = uc.cbcataid.Value;
                if (CheckBox_pub.Checked)
                {
                    l.isprivate.Value = RuleCode.Enum.NoYes.Yes;
                }
                else
                {
                    l.isprivate.Value = RuleCode.Enum.NoYes.No;
                }
                l.area.Value = AliApi.appInstanceId;
                l.ip.Value = zfunction.IPAddress;
                l.Insert();
                RuleCode.Table.cblist.afterinsert(l);
                Response.Redirect(ResolveUrl("~/blogview/") + l.cblistid.ToString() + ".html");
            }

        }

        protected void Button_cancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(ResolveUrl("~/blog/default.aspx"));
        }
        private void dealrequest()
        {
            if (AliApi.appUserId == "")
            {
                Response.Redirect(ResolveUrl("~/blog/default.aspx"));
            }
            HyperLink_default.NavigateUrl = ResolveUrl("~/blog/blogmanage.aspx?userid=") + AliApi.appDomainID;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if ((this.Request.Params["cblistid"] != null))
            {
                cblist l = cblist.Find(" cblistid = '" + this.Request.Params["cblistid"].ToString() + "' ");
                l.cbpath.Value = "";
                l.Modify();

                HyperLink_existfile.Visible = false;
                Button_clearfile.Visible = false;
                if (l.cbpath.ToString() != "")
                {
                    HyperLink_existfile.Visible = true;
                    HyperLink_existfile.NavigateUrl = l.cbpath.ToString();
                    HyperLink_existfile.Text = "现有附件:" + l.cbpath.ToString();
                    Button_clearfile.Visible = true;
                }
            }
        }
    }
}
